// 获取应用实例page
const app = getApp()
const common = require('./../../../utils/common.js');

Page({
    data: {
        user_data: null,
    },

    onLoad() {
      this.autoLogin();
    },

    onShow() {
      
    },

    //获取code
    async getLoginCode(){
        return new Promise((resolve, reject)=>{
            wx.login({
                success(res) {
                    if (res.code) {
                        resolve(res.code)
                    }else {
                        wx.showToast({
                            title: res.errMsg,
                            icon: 'none',
                            duration: 1500,
                        });
                        reject('失败');
                    }
                }
            })
        });
    },

    // 自动登录
    async autoLogin() {
        console.log('自动登录-autoLogin')
        let code = await this.getLoginCode()
        var result = common.request_api('/api/ums/miniCode/miniCodeLogin?code='+code+"&sysCode=XH_APP", {}, true);
        result.then(res_data => {
            console.log('res_data',res_data)
            try {
                if(res_data.code==200){
                    this.loginSuccessHandle(res_data);
                }
            } catch (e) {
                wx.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 1500,
                });
                return false;
            }
        });
    },

    //手机号登录绑定
    async PhoneLogin(e){
        let phone_code = e.detail.code
        let code = await this.getLoginCode()
        var result = common.request_api('/api/ums/miniCode/miniRegisterByPhone', {
            code: code,
            sysCode: "XH_APP",
            phoneCode:phone_code
        }, true);
        result.then(res_data => {
            try {
                if(res_data.code==200){
                    this.loginSuccessHandle(res_data);
                }
        
            } catch (e) {
                wx.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 1500,
                });
                return false;
            }
        });
    },

    //登录成功处理
    loginSuccessHandle(res_data){
        console.log('成功登陆',res_data.data)
        // 保存token
        wx.setStorageSync(common.tokenPrefix, res_data.data.token);
        wx.setStorageSync('alpha_user_id', res_data.data.userId);
        wx.setStorageSync('alpha_user_name', res_data.data.userName);
        const pages = getCurrentPages();
        if (pages.length > 1) {
          // 当前页面不是首页，可以回退
          wx.navigateBack({
            delta: 1 // 回退到上一个页面
          });
        }else{
            wx.navigateTo({
                url: '/alpha/pages/alpha_index/index',
            });
        }
    
    },

    // 样板列表
    goto_template_list: function (events) {
        console.log(events);
        // 跳转到样板列表
        wx.navigateTo({
            url: '/pages/template_list/template_list',
        });
    },

})
